package com.zsl.controller.User;

import com.alibaba.fastjson.JSON;
import com.zsl.service.OrderInfoService;
import com.zsl.service.impl.OrderInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "PayOrder", urlPatterns = "/pay/order")
public class PayOrder extends HttpServlet {
    OrderInfoService orderInfoService = new OrderInfoServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Map<String, Object> res = new HashMap<>();
        //处理过程
        //TODO 这个是测试用户，之后的用户会从Session中获取
        HttpSession session = request.getSession();
        String u_id=(String) session.getAttribute("id");
        try {
            res = orderInfoService.payShoppingCart(u_id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            res.put("code", "0");
            res.put("msg", "付款失败");
        } finally {
            PrintWriter out = response.getWriter();
            out.write(JSON.toJSONString(res));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
